package teste;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import model.Venda;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author Programador
 */
public class VendaTeste {
    
    public static void main(String args[]){
        Venda ven = new Venda();       
        ven.setTotal(1);
        ven.setPessoa(1);
        try {
            ven.setData(parseDateSQL("08/10/2012"));
        } catch (Exception ex) {
            Logger.getLogger(VendaTeste.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        Session sessao = HibernateUtil.getSession();
        Transaction t = sessao.beginTransaction();
        try {
            sessao.beginTransaction();
            sessao.save(ven);
            sessao.update(ven);
            t.commit();
            
        } catch (HibernateException erro) {
            erro.printStackTrace();
        }
        
    }
      
    public static java.sql.Date parseDateSQL(String data) throws Exception {
       if (data == null || data.equals("")) {
        return null;
        }
    java.util.Date date = null;
    try {
        DateFormat f = new SimpleDateFormat("dd/MM/yyyy");
        date = (java.util.Date) f.parse(data);
    } catch(ParseException e) {
    throw new Exception("Data com formato inválido");
        }
        return new java.sql.Date(date.getTime());
    } 
}
